define(['../vendor/kobako/pixi/pixi.js',], (PIXI) => {
  const node = {
    nodes: {},
    container: null,
    init,
  }

  function init_player() {
    let kobako = PIXI.kobako
    return kobako.loader.load_sheet_sprite('assets/marisa.png', 'assets/marisa.json')
      .then(marisa => {
        node.nodes['marisa'] = marisa
        marisa.set_ani('down_stop')
        kobako.sprites.move_by_keyboard(marisa, 5, kobako.keyboard)
        kobako.sprites.ani_by_keyboard(marisa, kobako.keyboard)
        // kobako.sprites.emit_by_keyboard(marisa, ' ', kobako.keyboard)
        kobako.sprites.emit_by_mouse(marisa, app.stage)
        return marisa
      })
  }

  function init_map() {
    let kobako = PIXI.kobako
    return kobako.loader.load_layers('assets/soto.png', 'assets/soto1.json', 2)
      .then(layers => {
        node.nodes['grass'] = layers[0]
        node.nodes['front-of-stone'] = layers[1]
        node.nodes['stone'] = layers[2]
        node.nodes['back-of-stone'] = layers[3]
        return layers
      })
  }

  function load() {
    node.container = new PIXI.Container()
    return init_player()
      .then(init_map)
      .then()
      .then(() => {
        node.nodes['stone'].addChild(node.nodes['marisa'])
        node.container.addChild(node.nodes['grass'])
        node.container.addChild(node.nodes['front-of-stone'])
        node.container.addChild(node.nodes['stone'])
        node.container.addChild(node.nodes['back-of-stone'])
      })
  }

  return node
})
